//
//  SettingViewModel.m
//  AppleMarket
//
//  Created by yangy on 31/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "SettingViewModel.h"
// model
#import "NewsModel.h"

@implementation SettingViewModel
- (instancetype)init
{
    self = [super init];
    if(self){
        [self.loginoutCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                [PlistCommon clearLoginModel];
                [kNotificationCenter postNotificationName:kUserDidLoginOutNotification object:nil];
                [SVProgressHUD showInfoWithStatus:@"退出成功"];
                [SVProgressHUD dismissWithDelay:3 completion:^{
                    [self.loginoutSuccesSubject sendNext:nil];
                }];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        } error:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
        
        [[self.loginoutCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
        
        [self.refershRateCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                [SVProgressHUD dismiss];
                NewsModel *model = [NewsModel yy_modelWithDictionary:[x objectForKey:@"Data"]];
                [self.refershRateEndSubject sendNext:model];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        } error:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
        
        [[self.refershRateCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
    }
    return self;
}

- (RACSubject *)loginClickSubject{
    if(!_loginClickSubject){
        _loginClickSubject = [RACSubject subject];
    }
    return _loginClickSubject;
}

- (RACSubject *)cellClickSubhject
{
    if(!_cellClickSubhject){
        _cellClickSubhject = [RACSubject subject];
    }
    return _cellClickSubhject;
}

- (RACCommand *)loginoutCommand
{
    if(!_loginoutCommand){
        _loginoutCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [NetRequest request_loginoutWithCompleteHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _loginoutCommand;
}

- (RACCommand *)refershRateCommand
{
    if(!_refershRateCommand){
        _refershRateCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [NetRequest request_GetDocModelById:5 completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _refershRateCommand;
}

- (RACSubject *)loginoutSuccesSubject
{
    if(!_loginoutSuccesSubject){
        _loginoutSuccesSubject = [RACSubject subject];
    }
    return _loginoutSuccesSubject;
}

- (RACSubject *)refershRateEndSubject
{
    if(!_refershRateEndSubject){
        _refershRateEndSubject = [RACSubject subject];
    }
    return _refershRateEndSubject;
}

@end
